package com.ellecity06.cwhiskymall.ui.calssify

import android.annotation.SuppressLint
import android.app.Activity
import android.app.AlertDialog
import android.app.Dialog
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.*
import android.widget.TextView
import androidx.annotation.NonNull
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.ellecity06.common.base.BaseAlertDialogBuilder
import com.ellecity06.common.base.CustomDialog
import com.ellecity06.common.ktx.launchActivity
import com.ellecity06.cwhiskymall.R
import com.ellecity06.cwhiskymall.adapter.ClassifyTopAdapter
import com.ellecity06.cwhiskymall.bean.response.ClassifyResponse
import com.ellecity06.cwhiskymall.ui.search.SearchActivity

/**
 *@author joe
 *@data 2024/10/28
 *@describe
 */
class ClassifyDialog : CustomDialog() {
    companion object {
        var listener: OnCheckListener? = null
        fun newInstance(
            check: Int,
            data: ArrayList<ClassifyResponse>,
            listener: OnCheckListener
        ): ClassifyDialog {
            val args = Bundle()
            args.apply {
                putSerializable("data", data)
                putInt("pos", check)
            }
            this.listener = listener
            val dialog = ClassifyDialog()
            dialog.arguments = args
            return dialog
        }
    }

    @SuppressLint("MissingInflatedId")
    @NonNull
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val v: View = LayoutInflater.from(activity).inflate(R.layout.d_classify_expand, null)
        val dialog: AlertDialog = BaseAlertDialogBuilder(requireActivity())
            .setView(v)
            .create()
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE) //取消标题显示
        dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))//透明背景
        dialog.window?.decorView?.setPadding(0, 0, 0, 0)
        val params = dialog.window?.attributes
        params?.width = WindowManager.LayoutParams.MATCH_PARENT
        dialog.window?.attributes = params
        dialog.window!!.setGravity(Gravity.TOP)
        val rv = v.findViewById<RecyclerView>(R.id.rv_list_classfy)
        val btnUp = v.findViewById<TextView>(R.id.btn_up)
        val search = v.findViewById<View>(R.id.layout_search)
        arguments?.let {
            var datas = it.getSerializable("data") as ArrayList<ClassifyResponse>
            var pos = it.getInt("pos")
            var topAdapter = ClassifyTopAdapter()
            var gridlayout = GridLayoutManager(requireContext(), 5)
            rv.layoutManager = gridlayout
            rv.adapter = topAdapter
            topAdapter.submitList(datas)
            topAdapter.notifyPos(pos)
            topAdapter.setOnItemClickListener { _, _, position ->
                listener?.check(position)
                dismiss()
            }
        }
        btnUp.setOnClickListener { dismiss() }
        search.setOnClickListener { launchActivity<SearchActivity>() }
        return dialog
    }

    override fun onDestroyView() {
        super.onDestroyView()
        listener = null
    }

    interface OnCheckListener {
        fun check(pos: Int)
    }
}